package com.it.gmall.all.controller;

import com.it.gmall.cart.client.CartFeignClient;
import com.it.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CartInfoController {
    @Autowired
    private CartFeignClient cartFeignClient;

    // 添加购物车-加
    // http://cart.gmall.com/addCart.html?skuId=' + this.skuId + '&skuNum
    @GetMapping("/addCart.html")
    public String addCart(long skuId, Integer skuNum, RedirectAttributes redirectAttributes,HttpServletRequest request){
        String userId = request.getHeader("userId");
        String userTempId = request.getHeader("userTempId");

        cartFeignClient.addCart(skuId,skuNum);
        // 传参数
        redirectAttributes.addAttribute("sId",skuId);
        redirectAttributes.addAttribute("sn",skuNum);
        // 加完之后重定向到查
        return "redirect:http://cart.gmall.com/toCart";
    }

    // 添加购物车-查
    @GetMapping("/toCart")
    public String toCart(long sId, Integer sn ,Model model){
        CartInfo cartInfo = cartFeignClient.toCart(sId);
        cartInfo.setSkuNum(sn); // 只显示本次添加的商品数量
        model.addAttribute("cartInfo",cartInfo);
        return "cart/addCart";
    }

    // 去购物车结算
    @GetMapping("/cart.html")
    public String cartList(){

        return "cart/index";
    }
}
